package com.java110.mall.cart.bmo.storeOrderCartReturn.impl;

import com.java110.mall.cart.bmo.storeOrderCartReturn.IUpdateStoreOrderCartReturnBMO;
import com.java110.core.annotation.Java110Transactional;
import com.java110.core.factory.GenerateCodeFactory;
import com.java110.dto.onlinePay.OnlinePayDto;
import com.java110.dto.storeOrder.StoreOrderCartDto;
import com.java110.dto.storeOrder.StoreOrderCartEventDto;
import com.java110.dto.storeOrder.StoreOrderCartReturnDto;
//import com.java110.intf.acct.IOnlinePayV1InnerServiceSMO;
import com.java110.intf.cart.*;
import com.java110.intf.goods.IProductSpecValueInnerServiceSMO;
import com.java110.po.onlinePay.OnlinePayPo;
import com.java110.po.product.ProductSpecValuePo;
import com.java110.po.storeOrder.StoreOrderCartPo;
import com.java110.po.storeOrder.StoreOrderCartEventPo;
import com.java110.po.storeOrder.StoreOrderCartReturnPo;
import com.java110.po.storeOrder.StoreOrderCartReturnEventPo;
import com.java110.utils.util.Assert;
import com.java110.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@Service("updateStoreOrderCartReturnBMOImpl")
public class UpdateStoreOrderCartReturnBMOImpl implements IUpdateStoreOrderCartReturnBMO {

    @Autowired
    private IStoreOrderCartReturnInnerServiceSMO storeOrderCartReturnInnerServiceSMOImpl;

    @Autowired
    private IStoreOrderCartReturnEventInnerServiceSMO storeOrderCartReturnEventInnerServiceSMOImpl;

    @Autowired
    private IStoreOrderCartEventInnerServiceSMO storeOrderCartEventInnerServiceSMOImpl;

    @Autowired
    private IStoreOrderCartInnerServiceSMO storeOrderCartInnerServiceSMOImpl;

    @Autowired
    private IStoreOrderInnerServiceSMO storeOrderInnerServiceSMOImpl;

    @Autowired
    private IProductSpecValueInnerServiceSMO productSpecValueInnerServiceSMOImpl;

//    @Autowired
//    private IOnlinePayV1InnerServiceSMO onlinePayV1InnerServiceSMOImpl;

    private static final String STATE_Y = "1100"; // 同意
    private static final String STATE_N = "1200"; //拒绝

    /**
     * @param storeOrderCartReturnPo
     * @return 订单服务能够接受的报文
     */
    @Java110Transactional
    public ResponseEntity<String> update(StoreOrderCartReturnPo storeOrderCartReturnPo) {

        StoreOrderCartReturnDto storeOrderCartReturnDto = new StoreOrderCartReturnDto();
        storeOrderCartReturnDto.setReturnId(storeOrderCartReturnPo.getReturnId());
        storeOrderCartReturnDto.setShopId(storeOrderCartReturnPo.getShopId());
        List<StoreOrderCartReturnDto> storeOrderCartReturnDtos
                = storeOrderCartReturnInnerServiceSMOImpl.queryStoreOrderCartReturns(storeOrderCartReturnDto);
        Assert.listOnlyOne(storeOrderCartReturnDtos, "退货单不存在");

        /**
         * 整单退单逻辑
         */
        if (StoreOrderCartReturnDto.RETURN_TYPE_ORDER.equals(storeOrderCartReturnDtos.get(0).getReturnType())) {

            return updateOrderReturn(storeOrderCartReturnPo, storeOrderCartReturnDtos.get(0));
        }


        StoreOrderCartDto storeOrderCartDto = new StoreOrderCartDto();
        storeOrderCartDto.setCartId(storeOrderCartReturnDtos.get(0).getCartId());
        storeOrderCartDto.setShopId(storeOrderCartReturnPo.getShopId());
        List<StoreOrderCartDto> storeOrderCartDtos
                = storeOrderCartInnerServiceSMOImpl.queryStoreOrderCarts(storeOrderCartDto);
        Assert.listOnlyOne(storeOrderCartDtos, "订单明细不存在");

        String state = storeOrderCartReturnPo.getState();

        if (STATE_Y.equals(state)) {
            storeOrderCartReturnPo.setState(StoreOrderCartReturnDto.STATE_Y);
        } else {
            storeOrderCartReturnPo.setState(StoreOrderCartReturnDto.STATE_N);
        }
        int flag = storeOrderCartReturnInnerServiceSMOImpl.updateStoreOrderCartReturn(storeOrderCartReturnPo);

        if (flag < 1) {
            return ResultVo.createResponseEntity(ResultVo.CODE_ERROR, "保存失败");

        }

        StoreOrderCartReturnEventPo storeOrderCartReturnEventPo = new StoreOrderCartReturnEventPo();
        storeOrderCartReturnEventPo.setEventId(GenerateCodeFactory.getGeneratorId(GenerateCodeFactory.CODE_PREFIX_eventId));
        storeOrderCartReturnEventPo.setEventMsg(storeOrderCartReturnPo.getMessage());
        storeOrderCartReturnEventPo.setEventObjId(storeOrderCartReturnDtos.get(0).getPersonId());
        storeOrderCartReturnEventPo.setEventObjType("U");//用户申请
        storeOrderCartReturnEventPo.setReturnId(storeOrderCartReturnPo.getReturnId());
        storeOrderCartReturnEventPo.setStoreId(storeOrderCartReturnDtos.get(0).getStoreId());
        storeOrderCartReturnEventPo.setShopId(storeOrderCartReturnPo.getShopId());

        flag = storeOrderCartReturnEventInnerServiceSMOImpl.saveStoreOrderCartReturnEvent(storeOrderCartReturnEventPo);
        if (flag < 1) {
            return ResultVo.createResponseEntity(ResultVo.CODE_ERROR, "保存失败");
        }


        //加入订单事件
        StoreOrderCartEventPo storeOrderCartEventPo = new StoreOrderCartEventPo();
        storeOrderCartEventPo.setEventId(GenerateCodeFactory.getGeneratorId(GenerateCodeFactory.CODE_PREFIX_eventId));
        storeOrderCartEventPo.setEventMsg(storeOrderCartReturnPo.getMessage());
        storeOrderCartEventPo.setEventObjId(storeOrderCartReturnDtos.get(0).getPersonId());
        storeOrderCartEventPo.setEventObjType("U");//用户申请
        storeOrderCartEventPo.setEventType(StoreOrderCartEventDto.EVENT_TYPE_COMMON);
        storeOrderCartEventPo.setOrderId(storeOrderCartReturnDtos.get(0).getOrderId());
        storeOrderCartEventPo.setCartId(storeOrderCartReturnDtos.get(0).getCartId());
        storeOrderCartEventPo.setShopId(storeOrderCartReturnPo.getShopId());

        flag = storeOrderCartEventInnerServiceSMOImpl.saveStoreOrderCartEvent(storeOrderCartEventPo);
        if (flag < 1) {
            return ResultVo.createResponseEntity(ResultVo.CODE_ERROR, "保存失败");
        }


        StoreOrderCartPo storeOrderCartPo = new StoreOrderCartPo();
        storeOrderCartPo.setCartId(storeOrderCartReturnDtos.get(0).getCartId());
        if (STATE_Y.equals(state)) {
            storeOrderCartPo.setState(StoreOrderCartDto.STATE_RETURN_SUCCESS);
            //减去销售 数量和金额
            BigDecimal cartNum = new BigDecimal(storeOrderCartDtos.get(0).getCartNum());
            BigDecimal payPrice = new BigDecimal(storeOrderCartDtos.get(0).getPayPrice());
            cartNum = cartNum.subtract(new BigDecimal(storeOrderCartReturnDtos.get(0).getReturnCartNum())).setScale(2, BigDecimal.ROUND_HALF_UP);
            payPrice = payPrice.subtract(new BigDecimal(storeOrderCartReturnDtos.get(0).getReturnPayPrice())).setScale(2, BigDecimal.ROUND_HALF_UP);
            storeOrderCartPo.setCartNum(cartNum.doubleValue() + "");
            storeOrderCartPo.setPayPrice(payPrice.doubleValue() + "");
        } else {
            storeOrderCartPo.setState(StoreOrderCartDto.STATE_RETURN_FAIL);
        }
        storeOrderCartPo.setStoreId(storeOrderCartReturnDtos.get(0).getStoreId());
        storeOrderCartPo.setShopId(storeOrderCartReturnPo.getShopId());
        flag = storeOrderCartInnerServiceSMOImpl.updateStoreOrderCart(storeOrderCartPo);
        if (flag < 1) {
            return ResultVo.createResponseEntity(ResultVo.CODE_ERROR, "保存失败");
        }

        //更新库存中数量
        ProductSpecValuePo productSpecValuePo = new ProductSpecValuePo();
        productSpecValuePo.setStoreId(storeOrderCartReturnDtos.get(0).getStoreId());
        productSpecValuePo.setProductId(storeOrderCartDtos.get(0).getProductId());
        productSpecValuePo.setValueId(storeOrderCartDtos.get(0).getValueId());
        productSpecValuePo.setAddStock(storeOrderCartReturnDtos.get(0).getReturnCartNum());
        flag = productSpecValueInnerServiceSMOImpl.updateProductSpecValue(productSpecValuePo);
        if (flag < 1) {
            return ResultVo.createResponseEntity(ResultVo.CODE_ERROR, "保存失败");
        }
        //支付表改为退费状态
        doUpdateOnlinePay(storeOrderCartDtos.get(0).getOrderId(), OnlinePayDto.STATE_WT, "申请退费", storeOrderCartDtos.get(0).getPayPrice());
        return ResultVo.createResponseEntity(ResultVo.CODE_OK, "保存成功");
    }


    private void doUpdateOnlinePay(String orderId, String state, String message, String money) {
        OnlinePayPo onlinePayPo = new OnlinePayPo();
        onlinePayPo.setMessage(message.length() > 1000 ? message.substring(0, 1000) : message);
        onlinePayPo.setOrderId(orderId);
        onlinePayPo.setRefundFee(money);
        onlinePayPo.setState(state);
        //onlinePayV1InnerServiceSMOImpl.updateOnlinePay(onlinePayPo);
    }

    private ResponseEntity<String> updateOrderReturn(StoreOrderCartReturnPo storeOrderCartReturnPo, StoreOrderCartReturnDto storeOrderCartReturnDto) {

        String state = storeOrderCartReturnPo.getState();

        if (STATE_Y.equals(state)) {
            storeOrderCartReturnPo.setState(StoreOrderCartReturnDto.STATE_Y);
        } else {
            storeOrderCartReturnPo.setState(StoreOrderCartReturnDto.STATE_N);
        }
        int flag = storeOrderCartReturnInnerServiceSMOImpl.updateStoreOrderCartReturn(storeOrderCartReturnPo);

        if (flag < 1) {
            return ResultVo.createResponseEntity(ResultVo.CODE_ERROR, "保存失败");

        }

        StoreOrderCartReturnEventPo storeOrderCartReturnEventPo = new StoreOrderCartReturnEventPo();
        storeOrderCartReturnEventPo.setEventId(GenerateCodeFactory.getGeneratorId(GenerateCodeFactory.CODE_PREFIX_eventId));
        storeOrderCartReturnEventPo.setEventMsg(storeOrderCartReturnPo.getMessage());
        storeOrderCartReturnEventPo.setEventObjId(storeOrderCartReturnDto.getPersonId());
        storeOrderCartReturnEventPo.setEventObjType("U");//用户申请
        storeOrderCartReturnEventPo.setReturnId(storeOrderCartReturnPo.getReturnId());
        storeOrderCartReturnEventPo.setStoreId(storeOrderCartReturnDto.getStoreId());
        storeOrderCartReturnEventPo.setShopId(storeOrderCartReturnPo.getShopId());

        flag = storeOrderCartReturnEventInnerServiceSMOImpl.saveStoreOrderCartReturnEvent(storeOrderCartReturnEventPo);
        if (flag < 1) {
            return ResultVo.createResponseEntity(ResultVo.CODE_ERROR, "保存失败");
        }

        //查询
        StoreOrderCartDto storeOrderCartDto = new StoreOrderCartDto();
        storeOrderCartDto.setOrderId(storeOrderCartReturnDto.getOrderId());
        List<StoreOrderCartDto> storeOrderCartDtos = storeOrderCartInnerServiceSMOImpl.queryStoreOrderCarts(storeOrderCartDto);
        for (StoreOrderCartDto tmpStoreOrderCartDto : storeOrderCartDtos) {
            //更新库存中数量
            ProductSpecValuePo productSpecValuePo = new ProductSpecValuePo();
            productSpecValuePo.setStoreId(tmpStoreOrderCartDto.getStoreId());
            productSpecValuePo.setProductId(tmpStoreOrderCartDto.getProductId());
            productSpecValuePo.setValueId(tmpStoreOrderCartDto.getValueId());
            productSpecValuePo.setAddStock(tmpStoreOrderCartDto.getCartNum());
            flag = productSpecValueInnerServiceSMOImpl.updateProductSpecValue(productSpecValuePo);
            if (flag < 1) {
                return ResultVo.createResponseEntity(ResultVo.CODE_ERROR, "保存失败");
            }
        }


        StoreOrderCartPo storeOrderCartPo = new StoreOrderCartPo();
        storeOrderCartPo.setOrderId(storeOrderCartReturnDto.getOrderId());
        if (STATE_Y.equals(state)) {
            storeOrderCartPo.setState(StoreOrderCartDto.STATE_RETURN_SUCCESS);
            //减去销售 数量和金额
            storeOrderCartPo.setCartNum("0");
            storeOrderCartPo.setPayPrice("0");
        } else {
            storeOrderCartPo.setState(StoreOrderCartDto.STATE_RETURN_FAIL);
        }
        storeOrderCartPo.setStoreId(storeOrderCartReturnDto.getStoreId());
        storeOrderCartPo.setShopId(storeOrderCartReturnPo.getShopId());
        flag = storeOrderCartInnerServiceSMOImpl.updateStoreOrderCart(storeOrderCartPo);
        if (flag < 1) {
            return ResultVo.createResponseEntity(ResultVo.CODE_ERROR, "保存失败");
        }

        OnlinePayDto onlinePayDto = new OnlinePayDto();
        onlinePayDto.setOrderId(storeOrderCartReturnDto.getOrderId());
        List<OnlinePayDto> onlinePayDtos = null;//onlinePayV1InnerServiceSMOImpl.queryOnlinePays(onlinePayDto);
        if (onlinePayDtos == null || onlinePayDtos.size() < 1) {
            return ResultVo.createResponseEntity(ResultVo.CODE_OK, "保存成功");
        }

        //支付表改为退费状态
        doUpdateOnlinePay(storeOrderCartDtos.get(0).getOrderId(), OnlinePayDto.STATE_WT, "申请退费", onlinePayDtos.get(0).getTotalFee());

        return ResultVo.createResponseEntity(ResultVo.CODE_OK, "保存成功");
    }

}
